home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / admin / linuxcon.000 / linuxcon / linuxconf-1.6 / netconf / networks.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-02  |  3.4 KB  |  152 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdarg.h>
  4. #include <ctype.h>
  5. #include <netdb.h>
  6. #include "netconf.h"
  7. #include "../misc/misc.h"
  8. #include "../xconf/xconf.h"
  9.  
  10. extern NETCONF_HELP_FILE help_networks;
  11. static CONFIG_FILE f_networks (ETC_NETWORKS,help_networks,CONFIGF_MANAGED);
  12.  
  13. PUBLIC NETWORK::NETWORK(const char *buf)
  14. {
  15.     /* #Specification: /etc/networks / format
  16.         A networks file has the following format
  17.  
  18.         # comment
  19.         network_name ip_number [ aliases ] [ # comment ]
  20.  
  21.         When we read /etc/networks, we split the line in four parts, being
  22.         the name, the ip number, the aliases and the comment.
  23.  
  24.         Further, when collecting the comment, we try to keep even
  25.         the space between the last data and the #.
  26.  
  27.         We hope to be able to edit normal /etc/networks file and
  28.         rewrite it mostly respecting the original format.
  29.  
  30.         Blank line are also remembered as comment.
  31.     */
  32.     is_valid = 1;
  33.     const char *pt = str_skip(buf);
  34.     if (*pt == '#'){
  35.         comment.setfrom (buf);
  36.     }else if (*pt > ' ' ){
  37.         // First copy the name
  38.         char tmp[200];
  39.         char *ptd = tmp;
  40.         while (*pt > ' ') *ptd++ = *pt++;
  41.         *ptd = '\0';
  42.         name1.setfrom (tmp);
  43.         pt = str_skip (pt);
  44.         if (*pt > ' '){
  45.             ptd = tmp;
  46.             while (*pt > ' ') *ptd++ = *pt++;
  47.             *ptd = '\0';
  48.             ip_num.setfrom (tmp);
  49.             const char *end_ip = pt;
  50.             pt = str_skip (pt);
  51.             if (*pt == '#'){
  52.                 comment.setfrom (end_ip);
  53.             }else if (*pt > ' '){
  54.                 const char *begother = pt;
  55.                 while (*pt != '#' && *pt != '\0') pt++;
  56.                 if (*pt == '\0'){
  57.                     others.setfrom (begother);
  58.                 }else{
  59.                     while (isspace(pt[-1])) pt--;
  60.                     int lenother = (int)(pt-begother);
  61.                     memcpy (tmp,begother,lenother);
  62.                     tmp[lenother] = '\0';
  63.                     others.setfrom (tmp);
  64.                     comment.setfrom (pt);
  65.                 }
  66.             }
  67.         }else{
  68.             is_valid = 0;
  69.         }
  70.     }else if (pt[0] != '\0'){
  71.         // Anything else than a blank link is an error at this point
  72.         is_valid = 0;
  73.     }
  74. }
  75. PUBLIC NETWORK::NETWORK(
  76.     const char *_ip_num,
  77.     const char *_name1,
  78.     const char *_others,
  79.     const char *_comment) : HOST (_ip_num,_name1,_others,_comment)
  80. {
  81. }
  82. PUBLIC NETWORK::NETWORK()
  83. {
  84. }
  85. /*
  86.     Output one NETWORK record in ascii
  87. */
  88. PUBLIC void NETWORK::print (FILE *fout) const
  89. {
  90.     if (!name1.is_empty())  fprintf (fout,"%s",name1.get());
  91.     if (!ip_num.is_empty())  fprintf (fout,"\t%s",ip_num.get());
  92.     if (!others.is_empty())  fprintf (fout,"\t%s",others.get());
  93.     if (!comment.is_empty()){
  94.         char *pt = str_skip(comment.get());
  95.         if (*pt != '#') fputs (" # ",fout);
  96.         fprintf (fout,"%s",pt);
  97.     }
  98.     fputc ('\n',fout);
  99. }
  100.  
  101. /*
  102.     Return !- 0 if this is a special entry of /etc/networks
  103.     This entry is special because it must exist for this program
  104.     expect those to work.
  105. */
  106. PUBLIC int NETWORK::is_special() const
  107. {
  108.     const char *name = getname1();
  109.     static char *tb[]={
  110.         NAM_ETH0_NETWORK,    NAM_ETH0_NETMASK,
  111.         NAM_ETH1_NETWORK,    NAM_ETH1_NETMASK,
  112.         NAM_ETH2_NETWORK,    NAM_ETH2_NETMASK,
  113.         NAM_ETH3_NETWORK,    NAM_ETH3_NETMASK,
  114.     };
  115.     int ret=0;
  116.     for (unsigned i=0; i<sizeof(tb)/sizeof(tb[0]); i++){
  117.         if (strcmp(tb[i],name)==0){
  118.             ret = 1;
  119.             break;
  120.         }
  121.     }
  122.     return ret;
  123. }
  124.  
  125. PUBLIC NETWORKS::NETWORKS()
  126. {
  127.     cfgf = &f_networks;
  128. }
  129. PUBLIC VIRTUAL HOST *NETWORKS::newhost (
  130.     const char *_ip_num,
  131.     const char *_name1,
  132.     const char *_others,
  133.     const char *_comment)
  134. {
  135.     return new NETWORK (_ip_num,_name1,_others,_comment);
  136. }
  137. PUBLIC VIRTUAL HOST *NETWORKS::newhost (const char *buf)
  138. {
  139.     return new NETWORK (buf);
  140. }
  141. #ifdef TEST
  142.  
  143. int main (int , char *[])
  144. {
  145.     NETWORKS hosts;
  146.     hosts.read ();
  147.     hosts.write ();
  148.     return 0;
  149. }
  150. #endif
  151.  
  152.